//
//  PywValidPhoneViewController.m
//  PywSdk
//
//  Created by smallwave on 16/8/26.
//  Copyright © 2016年 zero. All rights reserved.
//

#import "PywValidPhoneViewController.h"
#import "PywTextFieldView.h"
#import "UIButton+Pyw.h"
#import "UIImage+Pyw.h"
#import "PywMasonry.h"
#import "PywCommonManager.h"
#import "PywMBProgressHUD+Pyw.h"
#import "PywGetSmsCodeApi.h"
#import "PywSdk+login.h"
#import "PywValidSmsCodeViewController.h"
#import "MutableDictionary.h"

@interface PywValidPhoneViewController ()

@property (nonatomic, strong) PywTextFieldView  *phoneTextView; // 手机号码输入框
@property (nonatomic, strong) UIButton          *validCodeBtn;  // 获取验证码按钮

@property (nonatomic, strong) PywGetSmsCodeApi  *getSmsCodeApi; // 获取验证码请求
@property (nonatomic, copy) NSString            *smsType;       // 获取验证码场景类型

@end

@implementation PywValidPhoneViewController
KeyBoardHelper(90)

#pragma mark - Lift Cycle Method
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setUp];
}

#pragma mark - UI Interface
- (void)setUp{
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"验证手机";
    self.navigationItem.rightBarButtonItem = nil;
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self addSubviewAndLayoutSubviews];
}
- (void)addSubviewAndLayoutSubviews{
    
    [self.view addSubview:self.phoneTextView];
    [self.view addSubview:self.validCodeBtn];
    
    __weak typeof(self) wSelf = self;
    [self.phoneTextView mas_makeConstraints:^(PywMASConstraintMaker *make) {
        make.centerX.offset(0);
        make.top.equalTo(wSelf.view.mas_top).offset(80);
        make.width.equalTo(wSelf.view.mas_width).multipliedBy(0.85);
        make.height.equalTo(@40);
    }];
    
    [self.validCodeBtn mas_makeConstraints:^(PywMASConstraintMaker *make) {
        make.centerX.offset(0);
        make.top.equalTo(wSelf.phoneTextView.mas_bottom).offset(40);
        make.width.height.equalTo(wSelf.phoneTextView);
    }];
}

#pragma mark - Action
- (void)getValidCodeCilck{
    // 判断输入手机号 是否合法
    NSString *phoneNum = [self.phoneTextView.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    if (phoneNum.length <= 0) {
        
        [PywMBProgressHUD showError:@"请填写手机号" toView:self.view];
    } else if (![[PywCommonManager manager] validatePhone:self.phoneTextView.text]){
        
        [PywMBProgressHUD showError:@"手机号码不合法！" toView:self.view];
    } else {
        
        [self.getSmsCodeApi requestGetSmsCodeWithParams:[self requestCheckPhoneParams] success:^(NSString *status, id anyObjct) {
            // 跳转控制器
            PywValidSmsCodeViewController *validCodeVC = [[PywValidSmsCodeViewController alloc] initWithPhoneNumber:self.phoneTextView.text smsType:self.smsType];
            [self.navigationController pushViewController:validCodeVC animated:YES];
        } fail:^(NSError *error) {
            
        }];
    }
    
}

/**
 *  验证密保手机  传入的参数
 *
 *  @return <#return value description#>
 */
- (MutableDictionary *)requestCheckPhoneParams{
    NSString *phoneNumber = [self.phoneTextView.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    if ([PywSdk manger].isLogined) {
        self.smsType = [NSString stringWithFormat:@"%d",SMSCODETYPE_LOGIN_FORGETPWD]; // 已登录时
    } else {
        self.smsType = [NSString stringWithFormat:@"%d",SMSCODETYPE_NOTLOGIN_FORGETPWD];// 未登录时
    }
    
    MutableDictionary *params = [[MutableDictionary alloc] init];
    [params setObject:phoneNumber forKey:@"phone"];
    [params setObject:self.smsType forKey:@"smstype"];
    return params;
}

#pragma mark - lazy
- (PywTextFieldView *)phoneTextView{
    if (!_phoneTextView) {
        _phoneTextView = [PywTextFieldView phoneTextFieldWithLeftImg:@"icon_phone_normal"
                                                     TextPlaceholder:@"请输入您的手机号码"];
    }
    return _phoneTextView;
}
- (UIButton *)validCodeBtn{
    if (!_validCodeBtn) {
        _validCodeBtn = [UIButton buttonWithBackgroundColor:RGB(252, 177, 14, 1)
                                               cornerRadius:20
                                                 titleColor:[UIColor whiteColor]
                                         titleLabelFontSize:18
                                                      title:@"获取验证码"];
        
        [_validCodeBtn addTarget:self action:@selector(getValidCodeCilck) forControlEvents:UIControlEventTouchUpInside];
    }
    return _validCodeBtn;
}
- (PywGetSmsCodeApi *)getSmsCodeApi{
    if (!_getSmsCodeApi) {
        _getSmsCodeApi = [PywGetSmsCodeApi manager];
    }
    return _getSmsCodeApi;
}

@end
